package com.xq.cn.views;

import cn.hutool.core.bean.BeanUtil;
import com.xq.cn.bean.ChessPiecesNode;
import com.xq.cn.bean.ChineseChessNode;
import com.xq.cn.enums.TypeEnum;
import lombok.NonNull;

import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;
import java.util.List;

/**
 * 面板
 * JFrame中自带一个面板，但是阿哥面板有bug
 */
public class Panel extends JPanel {

    public int type; //棋盘类型
    public int width = 0; //布局宽度
    public int height = 0;//布局高度
    public int padding_x = 50; //布局横向缩进
    public int padding_y = 50;//布局纵向缩进

    public int width_layout = width - 2 * padding_x; //棋盘宽度
    public int height_layout = height - 2 * padding_y; //棋盘高度
    public double line_x = 15;//棋盘横向线条数量
    public double line_y = 15;//棋盘纵向线条数量

    public int line_width = 4; //棋盘线条宽度
    double ave_x = 0;//平均值x
    double ave_y = 0;//平均值y
    public boolean status = false;//true-还未结束，false-结束

    private List<ChessPiecesNode> list_CP; //所有节点

    public List<ChessPiecesNode> points_CP = new ArrayList<>();//双方占用所有

    private List<ChineseChessNode> list_CC; //所有节点

    public List<ChineseChessNode> points_CC = new ArrayList<>();//双方占用所有


    public List<Integer> black = new ArrayList<>();//黑棋

    public List<Integer> white = new ArrayList<>();//白棋 || 红旗
    public int point_type = 1; //黑-1，白-2
    private ChineseChessNode start;

    private ChineseChessNode end;

    private PropertyChangeSupport listeners = new PropertyChangeSupport(this);//监听

    private boolean initLayout = false;


    private int stepNum = 1;

    private Color colorNode = null;

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        super.addPropertyChangeListener(listener);
        listeners.addPropertyChangeListener(listener);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        super.removePropertyChangeListener(listener);
        listeners.removePropertyChangeListener(listener);
    }

    public Panel(@NonNull Integer type, Integer width, Integer height, Integer line_x, Integer line_y) {
        this.type = type;
        if (width != null) {
            this.width = width;
        } else {
            this.width = getWidth();
        }
        if (height != null) {
            this.height = height;
        } else {
            this.height = this.getWidth();
        }
        if (line_x != null) {
            this.line_x = line_x;
        }
        if (height != null) {
            this.line_y = line_y;
        }

        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (TypeEnum.GOMOKU.getType().equals(type)) {
                    mouseGoMoKu(e);
                } else if (TypeEnum.CHINESE_CHESS.getType().equals(type)) {
                    mouseChineseChess(e);
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }


    /**
     * 绘制面板内容的方法
     *
     * @param g the <code>Graphics</code> context in which to paint
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
//        if (!points_CC.isEmpty()) {
//            points_CC.clear();
//        }
        Graphics2D g2d = (Graphics2D) g;
        this.width = this.getWidth();
        this.height = this.getHeight();
        this.width_layout = width - 2 * padding_x;
        this.height_layout = height - 2 * padding_y;

        //设置背景颜色
        GradientPaint gradientPaint = new GradientPaint(0, 0, new Color(255, 248, 134), getWidth(), getHeight(), new Color(240, 114, 182));
        g2d.setPaint(gradientPaint);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        if (width > 0 && height > 0) {
            this.ave_x = (double) width_layout / (line_x - 1);
            this.ave_y = (double) height_layout / (line_y - 1);
            //设置线条颜色
            g2d.setColor(new Color(242, 234, 218));
            g2d.setStroke(new BasicStroke((float) line_width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

            for (double i = 1; i <= line_y; i++) { //10 横着画
                if (i == 1) {
                    g2d.drawLine(padding_x, padding_y, width_layout + padding_x, padding_y);
                } else if (i == line_y) {
                    g2d.drawLine(padding_x, height_layout + padding_y, width_layout + padding_x, height_layout + padding_y);
                } else {
                    g2d.drawLine(padding_x, (int) (ave_y * (i - 1)) + padding_y, width_layout + padding_x, (int) (ave_y * (i - 1)) + padding_y);
                }
            }
            for (double i = 1; i <= line_x; i++) { // 9
                if (i == 1) {
                    g2d.drawLine(padding_x, padding_y, padding_x, height_layout + padding_y);
                } else if (i == line_x) {
                    g2d.drawLine(width_layout + padding_x, padding_y, width_layout + padding_x, height_layout + padding_y);
                } else {
                    g2d.drawLine(padding_x + (int) (ave_x * (i - 1)), padding_y, padding_x + (int) (ave_x * (i - 1)), height_layout + padding_y);
                }
            }
            //五子棋
            if (TypeEnum.GOMOKU.getType().equals(type)) {
//                g2d.drawOval(x, y, radius, radius);//空心圆
                g2d.setColor(new Color(57, 63, 76));
                g2d.fillOval(padding_x + (int) (ave_x * 3) - line_width, padding_y + (int) (ave_y * 3) - line_width, line_width * 2, line_width * 2);//实心圆
                g2d.fillOval(padding_x + width_layout - (int) (ave_x * 3) - line_width, padding_y + (int) (ave_y * 3) - line_width, line_width * 2, line_width * 2);//实心圆
                g2d.fillOval(padding_x + (int) (ave_x * 3) - line_width, padding_y + height_layout - (int) (ave_y * 3) - line_width, line_width * 2, line_width * 2);//实心圆
                g2d.fillOval(padding_x + width_layout - (int) (ave_x * 3) - line_width, padding_y + height_layout - (int) (ave_y * 3) - line_width, line_width * 2, line_width * 2);//实心圆
                g2d.fillOval(padding_x + (width_layout / 2) - line_width, padding_y + (height_layout / 2) - line_width, line_width * 2, line_width * 2);//实心圆

                list_CP = initChessPiecesNode();

                Iterator<ChessPiecesNode> iterator = points_CP.iterator();
                while (iterator.hasNext()) {
                    ChessPiecesNode node = iterator.next();
                    g2d.setColor(node.getColor());
                    int radius = line_width * 10;
                    g2d.fillOval(node.getX() - (radius / 2), node.getY() - (radius / 2), radius, radius);//实心圆
                }
            }
            //象棋
            if (TypeEnum.CHINESE_CHESS.getType().equals(type)) {
                //设置楚河汉界背景颜色
                GradientPaint gradientPaintBg = new GradientPaint(padding_x + 2, padding_y + (int) (ave_y * 4) + 2, new Color(249, 212, 35), width_layout - 4, (int) ave_y - 4, new Color(248, 54, 0));
                g2d.setPaint(gradientPaintBg);
                g2d.fillRect(padding_x + 2, padding_y + (int) (ave_y * 4) + 2, width_layout - 4, (int) ave_y - 4);

                // 居中 设置楚河汉界文字颜色
                String word = "楚河 汉界";
                Font font = new Font("宋体", Font.BOLD, (int) (ave_y));
                g2d.setFont(font);
                FontMetrics metrics = g2d.getFontMetrics(font);
                int textX = (width / 2) - metrics.stringWidth(word) / 2;
                double v = (ave_y - metrics.getHeight());
                int textY = padding_y + (int) (ave_y * 5) + (int) v;
                g2d.setColor(new Color(149, 111, 41));
                g2d.drawString(word, textX, textY);

                //绘制交叉线
                g2d.setColor(new Color(242, 234, 218));
                g2d.drawLine(padding_x + (int) (ave_x * 3), padding_y, padding_x + (int) (ave_x * 5), padding_y + (int) (ave_y * 2));
                g2d.drawLine(padding_x + (int) (ave_x * 5), padding_y, padding_x + (int) (ave_x * 3), padding_y + (int) (ave_y * 2));
                g2d.drawLine(padding_x + (int) (ave_x * 3), padding_y + (int) (ave_y * 7), padding_x + (int) (ave_x * 5), padding_y + height_layout);
                g2d.drawLine(padding_x + (int) (ave_x * 5), padding_y + (int) (ave_y * 7), padding_x + (int) (ave_x * 3), padding_y + height_layout);

                //初始化棋子
                list_CC = initChineseChessNode();
                initChineseChess();
                Iterator<ChineseChessNode> iterator = points_CC.iterator();

                while (iterator.hasNext() && !initLayout) {
                    ChineseChessNode node = iterator.next();
                    if (node.getDisplay()) {
                        g2d.setColor(node.getColor());
                        int innerRadius = 10;
                        int outerRadius = 15;
                        int centerX = node.getX();
                        int centerY = node.getY();

                        // 绘制外圆
                        g2d.setStroke(new BasicStroke((float) line_width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
                        g2d.drawOval(centerX - outerRadius, centerY - outerRadius, 2 * outerRadius, 2 * outerRadius);

                        // 绘制内圆
                        g2d.setStroke(new BasicStroke((float) 2, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
                        Rectangle ellipseBounds = new Rectangle(centerX - innerRadius, centerY - innerRadius, 2 * innerRadius, 2 * innerRadius);
                        g2d.drawOval(ellipseBounds.x, ellipseBounds.y, ellipseBounds.width, ellipseBounds.height);

                        String text = node.getText();
                        Font fontItem = new Font("黑体", Font.BOLD, (ellipseBounds.width) - 5);
                        g2d.setFont(fontItem);
                        g2d.drawString(text, ellipseBounds.x + (ellipseBounds.width - g2d.getFontMetrics().stringWidth(text)) / 2, ellipseBounds.y + (ellipseBounds.height + g2d.getFontMetrics().getAscent()) / 2);
                    }
                }
            }
        }
    }


    protected List<ChessPiecesNode> initChessPiecesNode() {
        List<ChessPiecesNode> nodes = new ArrayList<>();
        for (int y = 1; y <= line_y; y++) {
            for (int x = 1; x <= line_x; x++) {
                int id = (int) ((y - 1) * line_y) + x;
                int point_x = padding_x + (int) ((x - 1) * ave_x);
                int point_y = padding_y + (int) ((y - 1) * ave_y);
                ChessPiecesNode node = new ChessPiecesNode();
                node.setId(id);
                node.setStatus(0);//默认没有被使用
                node.setX(point_x);
                node.setY(point_y);
                node.setMinX(point_x - (int) (ave_x / 2));
                node.setMaxX(point_x + (int) (ave_x / 2));
                node.setMinY(point_y - (int) (ave_y / 2));
                node.setMaxY(point_y + (int) (ave_y / 2));
                ChessPiecesNode byId = getCPNodeById(id);
                if (byId != null) {
                    node.setStatus(byId.getStatus());
                }
                nodes.add(node);
            }
        }
        return nodes;
    }


    protected List<ChineseChessNode> initChineseChessNode() {
        List<ChineseChessNode> nodes = new ArrayList<>();
        for (int y = 1; y <= line_y; y++) {
            for (int x = 1; x <= line_x; x++) {
                int id = (int) ((y - 1) * line_x) + x;
                int point_x = padding_x + (int) ((x - 1) * ave_x);
                int point_y = padding_y + (int) ((y - 1) * ave_y);
                ChineseChessNode node = new ChineseChessNode();
                node.setId(id);
                node.setStatus(0);//默认没有被使用
                node.setX(point_x);
                node.setY(point_y);
                node.setMinX(point_x - (int) (ave_x / 2));
                node.setMaxX(point_x + (int) (ave_x / 2));
                node.setMinY(point_y - (int) (ave_y / 2));
                node.setMaxY(point_y + (int) (ave_y / 2));
                ChineseChessNode byId = getCCNodeById(id);
                if (byId != null) {
                    node.setStatus(byId.getStatus());
                }
                nodes.add(node);
            }
        }
        return nodes;
    }


    /**
     * 根据Id获取坐标点
     *
     * @param id 坐标点
     * @return
     */
    public ChessPiecesNode getCPNodeById(Integer id) {
        ChessPiecesNode node = new ChessPiecesNode();
        if (list_CP != null) {
            list_CP.forEach(item -> {
                if (item.getId().equals(id)) {
                    BeanUtil.copyProperties(item, node, true);
                    item.setStatus(1);
                }
            });
            return node;
        } else {
            return null;
        }
    }


    public ChineseChessNode getCCNodeById(Integer id) {
        ChineseChessNode node = new ChineseChessNode();
        if (list_CC != null) {
            list_CC.forEach(item -> {
                if (item.getId().equals(id)) {
                    BeanUtil.copyProperties(item, node, true);
                    item.setStatus(1);
                }
            });
            return node;
        } else {
            return null;
        }
    }

    public ChineseChessNode getPointsCCNodeById(Integer id) {
        ChineseChessNode node = new ChineseChessNode();
        if (points_CC != null) {
            points_CC.forEach(item -> {
                if (item.getId().equals(id)) {
                    BeanUtil.copyProperties(item, node, true);
                    item.setStatus(1);
                }
            });
        }
        if (node.getId() == null) {
            return null;
        }
        return node;
    }


    public void updateCCNode(ChineseChessNode node) {
        if (list_CC != null) {
            list_CC.forEach(item -> {
                if (item.getId().equals(node.getId())) {
                    item.setStatus(1);
                    item.setText(node.getText());
                    item.setType(node.getType());
                }
            });
        }
    }

    public void updatePointsCCNode(ChineseChessNode node) {
        if (points_CC != null) {
            points_CC.forEach(item -> {
                if (item.getId().equals(node.getId())) {
                    item.setDisplay(node.getDisplay());
                    item.setColor(node.getColor());
                    item.setText(node.getText());
                }
            });
        }
    }

    /**
     * 获取棋盘中的节点
     *
     * @param x 坐标x
     * @param y 坐标y
     * @return
     */
    public ChessPiecesNode getCPNode(int x, int y) {
        ChessPiecesNode node = new ChessPiecesNode();
        list_CP.forEach(item -> {
            if (x >= item.getMinX() && x <= item.getMaxX() && y >= item.getMinY() && y <= item.getMaxY()) {
                BeanUtil.copyProperties(item, node, true);
                item.setStatus(1);
            }
        });
        return node;
    }

    public ChineseChessNode getCCNode(int x, int y) {
        ChineseChessNode node = new ChineseChessNode();
        list_CC.forEach(item -> {
            if (x >= item.getMinX() && x <= item.getMaxX() && y >= item.getMinY() && y <= item.getMaxY()) {
                BeanUtil.copyProperties(item, node, true);
                item.setStatus(1);
            }
        });
        return node;
    }


    /**
     * @param type 类型 黑棋/白棋
     * @param id   坐标点
     * @param step 步长
     * @return
     */
    public boolean verify(Integer type, Integer id, Integer step) {
        if (type == 1) {
            if (black.size() < 5) {
                return false;
            }
            List<Integer> arithmeticked = arithmeticSequence(id, step);
            arithmeticked.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 - o2;
                }
            });
            black.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 - o2;
                }
            });
            int size = arithmeticked.size();
            if (size < 5) {
                return false;
            }
            int i = size - 4;
            for (int j = 0; j < i; j++) {
                int tmp = j;
                List<Integer> piecesNodes = arithmeticked.subList(tmp, tmp + 5);
                if (black.containsAll(piecesNodes)) {
                    return true;
                }
            }
        } else if (type == 2) {
            if (white.size() < 5) {
                return false;
            }
            List<Integer> arithmeticked = arithmeticSequence(id, step);
            arithmeticked.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 - o2;
                }
            });
            white.sort(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 - o2;
                }
            });
            int size = arithmeticked.size();
            if (size < 5) {
                return false;
            }
            int i = size - 4;
            for (int j = 0; j < i; j++) {
                int tmp = j;
                List<Integer> piecesNodes = arithmeticked.subList(tmp, tmp + 5);
                if (white.containsAll(piecesNodes)) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 获取任意节点连成五子的所有可能
     *
     * @param id   坐标点
     * @param step 步长
     * @return
     */
    public List<Integer> arithmeticSequence(Integer id, Integer step) {
        //1、等差步长为step的等差数列
        List<Integer> listStep = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            listStep.add(id + ((i - 1) * step));
        }
        for (int i = 1; i <= 5; i++) {
            int num = id + ((i - 1) * (-step));
            if (num <= 0) {
                break;
            }
            listStep.add(num);
        }
        return listStep;
    }

    /**
     * 五子棋操作
     *
     * @param e
     */
    public void mouseGoMoKu(MouseEvent e) {

        ChessPiecesNode node = getCPNode(e.getX(), e.getY());
        JDialog dialog = new JDialog();
        dialog.setTitle("消息提醒");
        dialog.setSize(300, 100);
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setLayout(new BorderLayout());
        dialog.setUndecorated(true);//设置无边框


        if (node.getStatus() == 1) {
//                    JOptionPane.showMessageDialog(null, "");
            // 创建一个标签来显示消息
            JLabel label = new JLabel("禁止重复落子", SwingConstants.CENTER);
            dialog.add(label, BorderLayout.CENTER);
            //定时关闭dialog
            int TIME_VISIBLE = 2000;//单位ms
            Timer timer = new Timer(TIME_VISIBLE, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.dispose();
                }
            });
            timer.start();
            // 显示对话框
            dialog.setVisible(true);
            return;
        }
        if (point_type == 1) {
            //黑棋
            node.setColor(new Color(22, 24, 35));
            node.setType(1);
            points_CP.add(node);
            point_type = 2;
            black.add(node.getId());

            Integer[] arr = {1, (int) line_x - 1, (int) line_x, (int) line_x + 1};
            for (int i = 0; i < arr.length; i++) {
                boolean verify = verify(1, node.getId(), arr[i]);
                if (verify) {
                    // 创建一个标签来显示消息
                    JLabel label = new JLabel("黑棋获胜", SwingConstants.CENTER);
                    dialog.add(label, BorderLayout.CENTER);
                    //定时关闭dialog
                    int TIME_VISIBLE = 2000;//单位ms
                    Timer timer = new Timer(TIME_VISIBLE, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                        }
                    });
                    timer.start();
                    // 显示对话框
                    dialog.setVisible(true);
                    boolean oldStatus = status;
                    status = verify;
                    listeners.firePropertyChange("status", oldStatus, status);

                    break;
                }
            }
        } else if (point_type == 2) {
            node.setColor(new Color(233, 231, 239));
            node.setType(2);
            points_CP.add(node);
            point_type = 1;
            white.add(node.getId());
            Integer[] arr = {1, 14, 15, 16};
            for (int i = 0; i < arr.length; i++) {
                boolean verify = verify(2, node.getId(), arr[i]);
                if (verify) {
                    // 创建一个标签来显示消息
                    JLabel label = new JLabel("白棋获胜", SwingConstants.CENTER);
                    dialog.add(label, BorderLayout.CENTER);
                    //定时关闭dialog
                    int TIME_VISIBLE = 2000;//单位ms
                    Timer timer = new Timer(TIME_VISIBLE, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                        }
                    });
                    timer.start();
                    // 显示对话框
                    dialog.setVisible(true);
                    boolean oldStatus = status;
                    status = verify;
                    listeners.firePropertyChange("status", oldStatus, status);
                    break;
                }
            }
        }
        repaint();

    }


    public void mouseChineseChess(MouseEvent e) {
        ChineseChessNode ccNode = getCCNode(e.getX(), e.getY());
        ChineseChessNode byId = getPointsCCNodeById(ccNode.getId());
        if (start == null) {
            if (byId == null) {
                return;
            }
            start = byId;
            if (stepNum == 1 && start.getType() != 2) {//
                dialog("红棋先走");
                start = null;
                return;
            } else if (stepNum % 2 == 0 && start.getType() == 2) {
                dialog("黑棋走");
                start = null;
                return;
            } else if (stepNum % 2 != 0 && start.getType() == 1) {
                dialog("红棋走");
                start = null;
                return;
            }
            colorNode = start.getColor();
            start.setColor(Color.CYAN);
            start.setDisplay(true);
            updatePointsCCNode(start);
            end = null;
        } else if (end == null) {
            if (ccNode.getId().equals(start.getId()) || (byId != null && byId.getType().equals(start.getType()))) {
                return;
            }
            if (!route(ccNode.getId())) {
                return;
            }

            //隐藏被吃的棋子
            if (byId != null) {
                verify(byId);
            }

            start.setColor(colorNode);
            start.setDisplay(false);
            updatePointsCCNode(start);
            //完成业务逻辑后重置start
            end = ccNode;
            if (start.getId().equals(end.getId())) {
                dialog("重复操作");
            }
            end.setType(start.getType());
            end.setText(start.getText());
            end.setStatus(1);
            end.setColor(colorNode);
            end.setDisplay(true);
            points_CC.add(end);
            start = null;
            stepNum++;
        }
        repaint();
    }

    public void initChineseChess() {
        Integer[] arr_black = {1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 26, 28, 30, 32, 34, 36}; //黑
        List<Integer> list_black = List.of(arr_black);
        HashMap<Integer, String> map = getMap();
        map.forEach((key, value) -> {
            ChineseChessNode node = new ChineseChessNode();
            ChineseChessNode byId = getCCNodeById(key);
            BeanUtil.copyProperties(byId, node, true);
            node.setText(value);
            node.setDisplay(true);
            node.setStatus(1);
            if (list_black.contains(key)) {
                node.setColor(Color.BLACK);
                node.setType(1);
                updateCCNode(node);
                black.add(key);
            } else {
                node.setColor(Color.RED);
                node.setType(2);
                updateCCNode(node);
                white.add(key);
            }
            //防止重复添加
            ChineseChessNode ccNodeById = getPointsCCNodeById(key);
            if (ccNodeById == null) {
                points_CC.add(node);
            }
        });
    }

    private static HashMap<Integer, String> getMap() {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "车");
        map.put(2, "马");
        map.put(3, "象");
        map.put(4, "士");
        map.put(5, "将");
        map.put(6, "士");
        map.put(7, "象");
        map.put(8, "马");
        map.put(9, "车");
        map.put(20, "炮");
        map.put(26, "炮");
        map.put(28, "卒");
        map.put(30, "卒");
        map.put(32, "卒");
        map.put(34, "卒");
        map.put(36, "卒");
        map.put(55, "兵");
        map.put(57, "兵");
        map.put(59, "兵");
        map.put(61, "兵");
        map.put(63, "兵");
        map.put(65, "炮");
        map.put(71, "炮");
        map.put(82, "车");
        map.put(83, "马");
        map.put(84, "相");
        map.put(85, "士");
        map.put(86, "帅");
        map.put(87, "士");
        map.put(88, "相");
        map.put(89, "马");
        map.put(90, "车");
        return map;
    }


    public void verify(ChineseChessNode byId) {
        byId.setDisplay(false);
        removePointsCCNode(byId);
//        repaint();
    }

    public void removePointsCCNode(ChineseChessNode byId) {
        if (points_CC != null) {
            int size = points_CC.size();
            for (int i = 0; i < size; i++) {
                ChineseChessNode node = points_CC.get(i);
                if (node.getId().equals(byId.getId())) {
                    points_CC.remove(i);
                    if (node.getText().equals("将")) {
                        dialog("红旗胜");
                    } else if (node.equals("帅")) {
                        dialog("黑棋胜");
                    }
                    return;
                }
//                points_CC.remove(byId);
            }
        }
    }

    public boolean route(Integer end) {
        Integer id = start.getId();
        switch (start.getText()) {
            case "车":
                int line = id / 9;//第几行
                int index = id % 9;//第几个
                //横向
                ArrayList<Integer> listH = new ArrayList<>();
                if (index == 0) {
                    for (int i = 1; i < 9; i++) {
                        listH.add(id - i);
                    }
                } else if (index == 1) {
                    for (int i = 1; i < 9; i++) {
                        listH.add(id + i);
                    }
                } else {
                    for (int i = 1; i < 9 - index; i++) {
                        listH.add(id + i);
                    }
                    for (int i = 1; i < index; i++) {
                        listH.add(id - i);
                    }
                }
                //竖向
                ArrayList<Integer> listV = new ArrayList<>();
                if ((line == 1 && index == 0) || line == 0) {
                    for (int i = 1; i < 10; i++) {
                        listV.add(id + (i * 9));
                    }
                } else if ((line == 10 && index == 0) || line == 9) {
                    for (int i = 1; i < 10; i++) {
                        listV.add(id - (i * 9));
                    }
                } else {
                    for (int i = 1; i < 10 - (line + 1); i++) {
                        listV.add(id + (i * 9));
                    }
                    for (int i = 1; i < line + 1; i++) {
                        listV.add(id - (i * 9));
                    }
                }
                //判断是否可以落子
                if (listH.contains(end)) {
                    int num = 0;
                    if (id < end) {
                        for (int i = id + 1; i < end; i++) {
                            ChineseChessNode byId = getPointsCCNodeById(i);
                            if (byId != null && byId.getDisplay()) {
                                num++;
                            }
                        }
                    }
                    if (id > end) {
                        for (int i = id - 1; i > end; i--) {
                            ChineseChessNode byId = getPointsCCNodeById(i);
                            if (byId != null && byId.getDisplay()) {
                                num++;
                            }
                        }
                    }
                    if (num >= 1) {
                        dialog("横向：不可落子,中间隔有" + num + "棋子");
                        return false;
                    }
                } else if (listV.contains(end)) {
                    int num = 0;
                    if (id < end) {
                        for (int i = id + 9; i < end; i = i + 9) {
                            ChineseChessNode byId = getPointsCCNodeById(i);
                            if (byId != null && byId.getDisplay()) {
                                num++;
                            }
                        }
                    }
                    if (id > end) {
                        for (int i = id - 9; i > end; i = i - 9) {
                            ChineseChessNode byId = getPointsCCNodeById(i);
                            if (byId != null && byId.getDisplay()) {
                                num++;
                            }
                        }
                    }
                    if (num >= 1) {
                        dialog("纵向：不可落子,中间隔有" + num + "棋子");
                        return false;
                    }
                } else {
                    dialog("车只能走直线，不可在此落子");
                    return false;
                }
                break;
            case "马":

                break;
        }
        return true;
    }

    public void dialog(String msg) {
        JDialog dialog = new JDialog();
        dialog.setTitle("消息提醒");
        dialog.setSize(300, 100);
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setLayout(new BorderLayout());
        dialog.setUndecorated(true);//设置无边框
        JLabel label = new JLabel(msg, SwingConstants.CENTER);
        dialog.add(label, BorderLayout.CENTER);
        //定时关闭dialog
        int TIME_VISIBLE = 2000;//单位ms
        Timer timer = new Timer(TIME_VISIBLE, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });
        timer.start();
        // 显示对话框
        dialog.setVisible(true);
    }

}
